﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Item;
using Item.Buff;
using Battle;
using LitJson;

namespace ItemComponent
{
    /// <summary>
    /// 发射aoe弹幕
    /// </summary>
    public class Gun_Aoe : Gun
    {
        protected const string Key_AOEData = "AoeData";

#if UNITY_EDITOR
        [SerializeField, ReadOnly]
        private ST_AOEData debugAOEData;
#endif

        public ST_AOEData AOEData { get; protected set; }
        public ST_AOEData CurAOEData { get; protected set; }

        public override void InitComponent(JsonData data, Item.ItemBase item)
        {
            if (data == null || data[Key_AOEData] == null) return;

            ST_AOEData tmp = JsonMapper.ToObject<ST_AOEData>(data[Key_AOEData].ToJson());

            AOEData = tmp;
            CurAOEData = tmp;
#if UNITY_EDITOR
            debugAOEData = tmp;
#endif
            base.InitComponent(data,item);
        }

        public override void OnBuffChange(STBuffEffectData buffEffectData)
        {
            base.OnBuffChange(buffEffectData);

            var aoeData= AOEData;

            aoeData.dSizeX *= buffEffectData.fRange;
            aoeData.dSizeY *= buffEffectData.fRange;

            CurAOEData= aoeData;

#if UNITY_EDITOR
            debugAOEData= CurAOEData;
#endif
        }

        protected override async void FireAsync(ICanHitObj target)
        {
            if (target == null) return;
            Bullet_Aoe bullet_Aoe= await BulletFactory.CreateBulletAsync<Bullet_Aoe>(GetBulletData(target), BulletFactory.AOEPath);
            bullet_Aoe?.SetAoeSize(new Vector2((float)CurAOEData.dSizeX, (float)CurAOEData.dSizeY));
        }

#if UNITY_EDITOR
        public static STMyComponetData GetMyComponetData(ST_GunData gunData, ST_AOEData aoeData)
        {
            STMyComponetData componentData = GetMyComponetData(gunData);
            componentData.strType = typeof(Gun_Aoe).FullName;
            componentData.data[Key_AOEData] = JsonMapper.ToObject(JsonMapper.ToJson(aoeData));

            return componentData;
        }
#endif
    }
}
